<?php
// $Id: template.php,v 1.4.2.1 2007/04/18 03:38:59 drumm Exp $


// Custom Print Functions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------

/**
 * Creates a theme using the colorbar.swf and generates a non-flash variant.
 */
function entropy_print_masthead() {

  $no_flash = "<h1><a href='/' title='Digital FX'>Digtal FX</a></h1>";
  $no_flash .= theme('numbered_links', menu_primary_links(), array('class' => 'links primary-links'));
  
  $colorbar = check_url(base_path() . path_to_theme() . "/flash/colorbar/colorbar.swf");
  
  $params = array(
    'width' => "100%",
    'height' => 50,
    'no_flash' => $no_flash,
    'version' => variable_get('swfoa_version', '8'),
  );
  
  $vars = array(
    'themePath' => check_url(base_path() . path_to_theme()),
  );
  
  return theme("swfobject_api", $colorbar, $params, $vars);
}

/**
 * Create a theme for the old website.
 */
function entropy_print_oldsite() {
  
  $no_flash = "<span id='no-flash-images'>";
  $no_flash .= "<a href='mailto:greg@digitalfx.tv'><img src='old/contact.jpg' width='370' height='149' /></a>";
  $no_flash .= "<a href='http://www.adobe.com/products/flashplayer'><img src='old/no-flash.jpg' width='373' height='149' /></a>";
  $no_flash .= "</span>";
  
  $oldsite = check_url("old/bedrock.swf");

  $params = array(
    'width' => 768,
    'height' => 400,
    'no_flash' => $no_flash,
  );
  
  return theme("swfobject_api", $oldsite, $params);
}

/**
 * Displays Taxonomy Terms broken out by Vocabulary.
 * @return HTML formated list of Taxonomy terms.
 */
 function entropy_print_terms($nid) {
   $vocabularies = taxonomy_get_vocabularies();
   $output = '';
   
   foreach($vocabularies as $vocabulary) {
     if ($vocabularies) {
       $terms = taxonomy_node_get_terms_by_vocabulary($nid, $vocabulary->vid);
       if ($terms) {
         $links = array();
         $output .= '<h3>' . $vocabulary->name . ':</h3>';
         $output .= '<p>';
         foreach ($terms as $term) {
           $links[] = l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
         }
         $output .= implode(' | ', $links);
         $output .= '</p>';
       }
     }
   }
   return $output;
}

/**
 * Custom Link Theme Function
 */
function entropy_numbered_links($links, $attributes = array('class' => 'links')) {
  $output = '';

  if (count($links) > 0) {
    $output = '<ul'. drupal_attributes($attributes) .'>';

    $num_links = count($links);
    $i = 1;

    foreach ($links as $key => $link) {
      $class = '';

      // Automatically add a class to each link and also to each LI
      if (isset($link['attributes']) && isset($link['attributes']['class'])) {
        $link['attributes']['class'] .= ' ' . $key;
        $class = $key;
      }
      else {
        $link['attributes']['class'] = $key;
        $class = $key;
      }

      // Add first and last classes to the list of links to help out themers.
      $extra_class = 'number-' . $i . ' ';
      if ($i % 2 == 0) {
        $extra_class .= 'even ';
      }
      else {
        $extra_class .= 'odd ';
      }
      $output .= '<li class="'. $extra_class . $class .'">';

      // Is the title HTML?
      $html = isset($link['html']) && $link['html'];

      // Initialize fragment and query variables.
      $link['query'] = isset($link['query']) ? $link['query'] : NULL;
      $link['fragment'] = isset($link['fragment']) ? $link['fragment'] : NULL;

      if (isset($link['href'])) {
        $output .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html);
      }
      else if ($link['title']) {
        //Some links are actually not links, but we wrap these in <span> for adding title and class attributes
        if (!$html) {
          $link['title'] = check_plain($link['title']);
        }
        $output .= '<span'. drupal_attributes($link['attributes']) .'>'. $link['title'] .'</span>';
      }

      $i++;
      $output .= "</li>\n";
    }

    $output .= '</ul>';
  }

  return $output;
}

/**
 * Sets the center-tag class attribute.
 * Adds 'sidebar-left' or 'sidebar-right' as needed.
 */
function entropy_center_class($sidebar_left, $sidebar_right) {
  
  $class = "";
  
  if ($sidebar_left != '') {
    $class .= ' sidebar-left';
  }
  if ($sidebar_right != '') {
    $class .= ' sidebar-right';
  }

  print $class;
}

/**
 * Sets the body class to "front-page" if on the front-page (for different styles).
 */
function entropy_body_class() {
  if (drupal_is_front_page()) {
    return "front-page";
  }
}

/**
 * Override or insert PHPTemplate variables into the templates.
 */
function _phptemplate_variables($hook, $vars) {
  switch ($hook) {
  
    case 'page':
      if ($secondary = menu_secondary_local_tasks()) {
        $output = '<span class="clear"></span>';
        $output .= "<ul class=\"tabs secondary\">\n". $secondary ."</ul>\n";
        $vars['tabs2'] = $output;
      }
      // Hook into color.module
      if (module_exists('color')) {
        _color_page_alter($vars);
      }
      
      // Add swfobject.js to the Scripts list
      $so_path = drupal_get_path('module', 'swfobject_api');
      drupal_add_js($so_path .'/swfobject.js');
      
      // set $masthead
      $vars['masthead'] = entropy_print_masthead();
      
      // Create Old Site
      if (drupal_is_front_page()) {
        $vars['oldsite'] = entropy_print_oldsite();
      }
      else {
        /*
        // Load JSON Server
        json_load();
        
        $se_path = path_to_theme() . "/screen_effect";
        drupal_add_css($se_path . '/screen_effect.css');
        drupal_add_js($se_path .'/screen_effect.js', 'theme');
        */
      }
      
      // set Test Site Banner
      if ($_SERVER['SERVER_NAME'] != 'digitalfx.tv') {
        $vars['test_site'] = "<h1 id='test-site-banner'>Test Site (" . $_SERVER['SERVER_NAME'] . ")</h1>";
      }
      
      return $vars;
      
    default:
      return array();
  }
}





// Custom Module Themes
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------

function entropy_videofield($file, $item, $field, $node) {
  $file = (array)$file;
  
  if (is_file($file['filepath'])) {
    // Path to FLV
    if ($file['fid'] == 'upload') {
      $path = file_create_filename($file['filename'], file_create_path());
    } else {
      $path = $file['filepath'];
    }

    // Image Preview
    $image = node_load($node->iid);
    $preview = file_create_url($image->images['preview']);
    
  	// set up SWFObject
  	$player = check_url(base_path() . path_to_theme()  . "/flash/flvplayer/flvplayer.swf");
  	$params = array(
  		'width' => 640,
  		'height' => 500,
      'allowfullscreen' => 'true',
  	);
  	$vars = array(
  		'file' => check_url(base_path(). $path),
      'image' => $preview,
      'backcolor' => 0x938c78,
      'frontcolor' => 0x2F2C26,
      'lightcolor' => 0xFFE302,
  	);
    
    return theme("swfobject_api", $player, $params, $vars);
  }
}

function entropy_videofield_preview($file, $item, $field, $node) {
  $file = (array)$file;
  
  if (is_file($file['filepath'])) {

  	// Image Thumbnail
    $image = node_load($node->iid);
    $img_size = variable_get('image_attach_size_teaser_'. $node->type, 'thumbnail');
    $output = image_display($image, $img_size);
    
    return $output;
  }
}





// Leftover Garland Theme Functions
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------

/**
 * Returns the rendered local tasks. The default implementation renders them as tabs.
 */
function phptemplate_menu_local_tasks() {
  $output = '';

  if ($primary = menu_primary_local_tasks()) {
    $output .= "<ul class=\"tabs primary\">\n". $primary ."</ul>\n";
  }

  return $output;
}

/**
 * Allow themable wrapping of all comments.
 */
function phptemplate_comment_wrapper($content, $type = null) {
  static $node_type;
  if (isset($type)) $node_type = $type;

  if (!$content || $node_type == 'forum') {
    return '<div id="comments">'. $content . '</div>';
  }
  else {
    return '<div id="comments"><h2 class="comments">'. t('Comments') .'</h2>'. $content .'</div>';
  }
}
